package com.guoxue.action.front;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.guoxue.domain.CoinsVo;
import com.guoxue.domain.PropertyConfigVo;
import com.guoxue.domain.ShopConfigVo;
import com.guoxue.domain.UserCoinsVo;
import com.guoxue.model.ShopConfig;
import com.guoxue.service.ICoinsService;
import com.guoxue.service.IPropertyConfigService;
import com.guoxue.service.IShopConfigService;
import com.guoxue.service.IUserCoinsService;
import com.guoxue.util.LoggerUtil;
import com.guoxue.util.OSSUtil;

@Controller
@RequestMapping("shop")
/**
 * 商城资源加载处理接口，包括：
 * 1.getCoinsList获取商城金币充值列表
 * 2.getPropertyConfigList获取商城的所有商品信息列表
 * 3.getPropertyConfig获取单个商品信息资源内容
 * @author kanghb
 */
public class StoreAciton extends BaseAction {
	@Resource
	private ICoinsService coinsService;
	@Resource
	private IUserCoinsService userCoinsService;
	@Resource
	private IPropertyConfigService propertyConfigService;
	@Resource
	private OSSUtil oss;
	@Resource
	private IShopConfigService shopConfigService;
	/**
	 * 获取所有的充值金币信息
	 * @param req
	 * @param resp
	 * @param uid  用户id
	 * @return 
	 */
	@ResponseBody
	@RequestMapping(value="/getCoinsList")
	public  JSON getCoinsList(HttpServletRequest req, HttpServletResponse resp,Integer uid){
		try {
			if(null == uid){
				return Message(ERROR, "未提交用户id", null);
			}
			UserCoinsVo userCoins =  userCoinsService.getUserCoinsById(uid);
			if(null == userCoins){
				userCoins = new UserCoinsVo();
				userCoins.setUid(uid);
				userCoinsService.saveUserCoins(userCoins);
			}
			List<CoinsVo> coinsList = coinsService.getCoinsList();
			for(CoinsVo c:coinsList){
				c.setIcon(isEmpty(c.getIcon())?null:oss.getUrl(bucket, c.getIcon(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
			}
			JSONObject json = new JSONObject();
			json.put("coinsList",coinsList );
			json.put("coinsNumber", userCoins.getCoinsNumber());
			return Message(SUCCESS, "获取数据成功", json);
		} catch (Exception e) {
			e.printStackTrace();
			return LoggerUtil.logInfo(e, "获取数据失败");
		}
	}
	/**
	 * 获取商城的所有商品信息
	 * @param req
	 * @param resp
	 * @param uid 用户id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getPropertyConfigList")
	public  JSON getPropertyConfigList(HttpServletRequest req, HttpServletResponse resp,Integer uid){
		try {
			if(null == uid){
				return Message(ERROR, "未提交用户id", null);
			}
			List<ShopConfig> ShopConfigList = new ArrayList<>();
			List<PropertyConfigVo> propertyConfigVoList = propertyConfigService.getPropertyConfigList();
			for(PropertyConfigVo p :propertyConfigVoList){
				p.setPropIcon(isEmpty(p.getPropIcon())?null:oss.getUrl(bucket, p.getPropIcon(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
				ShopConfigVo shopConfigVo = shopConfigService.getShopConfig(p.getPropId());
				ShopConfig shopConfig = new ShopConfig(shopConfigVo.getPrice(),shopConfigVo.getBuyLimit(),p);
				ShopConfigList.add(shopConfig);
			}
			UserCoinsVo userCoins =  userCoinsService.getUserCoinsById(uid);
			if(null == userCoins){
				userCoins = new UserCoinsVo();
				userCoins.setUid(uid);
				userCoinsService.saveUserCoins(userCoins);
			}
			JSONObject json = new JSONObject();
			json.put("propertyConfig",ShopConfigList);
			json.put("coinsNumber", userCoins.getCoinsNumber());
			return Message(SUCCESS, "获取数据成功", json);
		} catch (Exception e) {
			return LoggerUtil.logInfo(e, "获取数据失败");
		}
	}
	
	/**
	 * 获取商城的单个商品信息
	 * @param req
	 * @param resp
	 * @param propId 商品propId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getPropertyConfig")
	public  JSON getPropertyConfig(HttpServletRequest req, HttpServletResponse resp,Integer propId){
		try {
			if(null == propId){
				return Message(ERROR, "未提交商品id", null);
			}
			PropertyConfigVo propertyConfigVob  = propertyConfigService.getPropertyConfigById(propId);
			propertyConfigVob.setPropIcon(isEmpty(propertyConfigVob.getPropIcon())?null:oss.getUrl(bucket, propertyConfigVob.getPropIcon(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
			return Message(SUCCESS, "获取数据成功", propertyConfigVob);
		} catch (Exception e) {
			return LoggerUtil.logInfo(e, "获取数据失败");
		}
	}
}
